﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CRM.Common
{
    public static class StringHelper
    {
        /// <summary>
        /// 根据“,”分隔字符串（待分隔字符串中的连续的“,,”视为转义为“,”）
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        public static string[] SplitOptions(string options)
        {
            if (options.IndexOf(",,") == -1)
            {
                return options.Split(',');
            }
            else
            {
                string tempPlaceHodler = "@" + Guid.NewGuid().ToString();

                while (options.IndexOf(tempPlaceHodler) != -1)
                {
                    tempPlaceHodler = "@" + Guid.NewGuid().ToString();
                }

                options = options.Replace(",,", tempPlaceHodler);

                string[] arr = options.Split(',');

                for (int i = 0; i < arr.Length; i++)
                {
                    arr[i] = arr[i].Replace(tempPlaceHodler, ",");
                }

                return arr;
            }
        }

        public static string CombieOptions(params string[] optionValues)
        {
            if (optionValues == null || optionValues.Length == 0)
            {
                return "";
            }

            StringBuilder sb = new StringBuilder(optionValues[0].Translate());

            for (int i = 1; i < optionValues.Length; i++)
            {
                sb.Append("," + optionValues[i].Translate());
            }

            return sb.ToString();
        }

        static string Translate(this string optionValue)
        {
            return optionValue.Replace(",", ",,");
        }
    }
}
